$(document).ready(function () {

    $("#button").bind("click", function () {
        $.ajax({
            type: "GET",
            url: "/first",
            success: function (result) {
                var data = $.parseJSON(result);
                var list = data.page.list;
                var len = list.length;
                var html = "";
                for (var i = 0; i < len; i++) {
                    html += "<tr>";
                    html += "<td>" + list[i].bulletin_name + "</td>"
                    html += "<td>" + list[i].exam_name + "</td>"
                    html += "<td>" + list[i].school_name + "</td>"
                    html += "<td>" + list[i].group_name + "</td>"
                    html += "<td>" + list[i].user_name + "</td>"
                    html += "<td>" + list[i].identity + "</td>"
                    html += "<td>" + list[i].mobile + "</td>"
                    html += "<td>" + list[i].tutor + "</td>"
                    html += '<td><button type="button" id="'+list[i].id+'" onclick="sendMsg('+"'"+list[i].id+"'"+')" class="btn btn-default">发送消息</button></td>';
                    html += "</tr>";
                }
                $("#templete").empty().append(html);

                //调用分页
                laypage({
                    cont: 'pagenation',
                    pages: data.page.pages ,//得到总页数
                    jump: function(obj){
                        if (obj.curr != 1) {
                            query(obj.curr);
                        }
                    }
                });

            },
            error: function (a,b,c) {
            }
        });
    });
    
    function query(pageNo) {
        $.ajax({
            type: "GET",
            url: "/first",
            data: {"page" : pageNo},
            success: function (result) {
                var data = $.parseJSON(result);
                var list = data.page.list;
                var len = list.length;
                var html = "";
                for (var i = 0; i < len; i++) {
                    html += "<tr>";
                    html += "<td>" + list[i].bulletin_name + "</td>"
                    html += "<td>" + list[i].exam_name + "</td>"
                    html += "<td>" + list[i].school_name + "</td>"
                    html += "<td>" + list[i].group_name + "</td>"
                    html += "<td>" + list[i].user_name + "</td>"
                    html += "<td>" + list[i].identity + "</td>"
                    html += "<td>" + list[i].mobile + "</td>"
                    html += "<td>" + list[i].tutor + "</td>"
                    html += '<td><button type="button" id="'+list[i].id+'" onclick="sendMsg('+"'"+list[i].id+"'"+')" class="btn btn-default">发送消息</button></td>';
                    html += "</tr>";
                }
                $("#templete").empty().append(html);
            },
            error: function (a,b,c) {
            }
        });
    }

    $("#button2").bind("click", function () {
        location.href = "/first?page=2&pageSize=10";
    });

    $("#tagbutton").bind("click", function () {
        location.href = "/tag";
    });

    $("#echartbutton").bind("click", function () {
        location.href = "/echart";
    });

    $("#logout").bind("click", function () {
        location.href = "/logout";
    });

    $("#faceAPI").bind("click", function () {
        $.ajax({
            type: "POST",
            dataType:"JSON",
            url: "/faceAPI",
            data: {"filePath": $("#filepath").val()},
            success: function (result) {
                layer.alert(result.data, {icon:1});
            },
            error: function (a,b,c) {
                layer.alert("没有权限执行操作",{icon:2});
            }
        });
    });

    $("#tesseract").bind("click", function () {
        $.ajax({
            type: "POST",
            dataType:"JSON",
            url: "/tesseract",
            data: {"filePath": $("#filepath").val()},
            success: function (result) {
                layer.alert(result.data, {icon:1});
            },
            error: function (a,b,c) {
                layer.alert("没有权限执行操作",{icon:2});
            }
        });
    });
});

function sendMsg(id) {
    var id = $("#" + id).attr("id");
    $.ajax({
        type: "POST",
        dataType:"JSON",
        url: "/sendMsg",
        data: {"id": id},
        success: function (result) {
            layer.alert("Topic:"+result.topic +" " + "MessageId:"+result.messageId,{icon:1});
        },
        error: function (a,b,c) {
            layer.alert("没有权限执行操作",{icon:2});
        }
    });
}